package org.ariadne_eu.hcifetcher.mumeUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.ariadne_eu.hcifetcher.rest.RestUtils;
import org.json.JSONException;
import org.json.JSONObject;

@Path("/entries/question")
public class Question {
	
	@Path("/")
	@GET
	@Produces("application/json")
	public String getQuestionsBySet(@QueryParam("courseid") String courseID,
									@QueryParam("studentid") String author,
									@QueryParam("size") String size,
									@QueryParam("offset") String offset) {
		int intoffset = 0, intsize = 1000;
		try{
			intsize = Integer.parseInt(size);
			intoffset = Integer.parseInt(offset);
		}
		catch(NumberFormatException e){
			if(size == null)
				intsize = 1000;
			else
				intoffset = 0;
		}
		Map<String, Object> filter = new HashMap<String, Object>();
		filter.put("courseid", courseID);
		filter.put("studentid", author);
		return RestUtils.getInstance().JSONToString(RestUtils.getInstance().getEntriesJSON("Question", filter, intoffset, intsize, "datestamp", false));
	}
	

	@Path("/withanswers")
	@GET
	@Produces("application/json")
	public String getQuestionsWithAnswers(@QueryParam("courseid") String courseID,
										  @QueryParam("studentid") String author,
										  @QueryParam("questionssize") String questionssize,
										  @QueryParam("questionssoffset") String questionsoffset,
										  @QueryParam("answerssize") String answerssize) {
		int intquestionsoffset = 0, intquestionssize = 1000, intanswerssize = 1000;
		try{
			intquestionssize = Integer.parseInt(questionssize);
			intquestionsoffset = Integer.parseInt(questionsoffset);
			intanswerssize = Integer.parseInt(answerssize);
		}
		catch(NumberFormatException e){
		}
		return getQuestionsWithAnswers(courseID, author, intquestionssize, intquestionsoffset, intanswerssize);
	}
	
	@Path("/withinfo")
	@GET
	@Produces("application/json")
	public String getQuestionsWithAnswers(@QueryParam("courseid") String courseID,
										  @QueryParam("studentid") String author,
										  @QueryParam("size") String questionssize,
										  @QueryParam("offset") String questionsoffset) {
		int intquestionsoffset = 0, intquestionssize = 1000;
		try{
			intquestionssize = Integer.parseInt(questionssize);
			intquestionsoffset = Integer.parseInt(questionsoffset);
		}
		catch(NumberFormatException e){
		}
		return getQuestionsWithInfo(courseID, author, intquestionssize, intquestionsoffset);
	}
	
	public String getQuestionsWithAnswers(String courseID, String author,
			int questionssize, int questionsoffset, int answerssize) {
		Map<String, Object> filters = new HashMap<String, Object>();
		filters.put("course_id", courseID);
		filters.put("author", author);
		Vector<JSONObject> questions = RestUtils.getInstance().getEntriesJSON("Question", filters, questionsoffset, questionssize, "datestamp", false);
		for(JSONObject question : questions){
			Map<String, Object> answerFilter = new HashMap<String, Object>();
			try {
				answerFilter.put("questionid", question.get("id"));
				question.put("answers",RestUtils.getInstance().getEntriesJSON("Answer", answerFilter, 0, answerssize, "datestamp", true));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return RestUtils.getInstance().JSONToString(questions);
	}
	
	public String getQuestionsWithInfo(String courseID, String author,
			int questionssize, int questionsoffset) {
		Map<String, Object> filters = new HashMap<String, Object>();
		filters.put("courseid", courseID);
		filters.put("author", author);
		Vector<JSONObject> questions = RestUtils.getInstance().getEntriesJSON("Question", filters, questionsoffset, questionssize, "datestamp", false);
		for(JSONObject question : questions){
			try {
				question.append("student", RestUtils.getInstance().getEntityById(question.getString("studentid")));
				question.append("course", RestUtils.getInstance().getEntityById(question.getString("courseid")));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return RestUtils.getInstance().JSONToString(questions);
	}
	
	

}
